package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.jzfqwczzpfsn.service.IYwYnJzfqwczzPfsnbgService;
import com.augurit.gzcgwsp.jzfqwczzpfsn.web.form.YwYnJzfqwczzPfsnForm;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.readAndParseXML.service.IPfBgService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;
/**
 * 排放 变更
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional
public class PfBgServiceImpl implements IPfBgService{
	Logger logger = (Logger) LoggerFactory.getLogger(PfBgServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;

	@Autowired
	private IYwYnJzfqwczzPfsnbgService ywYnJzfqwczzPfsnbgService;
	
	//读取接口配置信息
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 
	// xml存放路径
	private final String pfBgfilepath = localResource.getString("pfBgfilepath");

	// 附件存放路径
	private final String pfBgfujianpath = localResource.getString("pfBgfujianpath");

	// 解析成功反馈xml存放路径
	private final String pfBgfeedbackPath = localResource.getString("pfBgfeedbackPath");

	// 解析异常xml存放路径
	private final String pfBgexceptionPath = localResource.getString("pfBgexceptionPath");

	// 解析后，xml备份
	private final String pfBgbackPath = localResource.getString("pfBgbackPath");

	private Map<String, String> pfBgKeyMap;// xml内容的key

	private Map<String, String> pfBgKeyMapHead;// xml头部的key

	private Map<String, Long> pfBgfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void initpfBgMap() {
		// 初始化数据部分对应的map
		pfBgKeyMap = new HashMap<String, String>();
		pfBgKeyMap.put("approvalNo", "approvalNo");
		pfBgKeyMap.put("businessCode", "businessCode");
		pfBgKeyMap.put("YSL_NO", "YSL_NO");
		pfBgKeyMap.put("sendTime", "sendTime");
		
		pfBgKeyMap.put("GCDZ", "GCDZ");
		pfBgKeyMap.put("SQDW", "SQDW");
		pfBgKeyMap.put("GCMC", "GCMC");
		pfBgKeyMap.put("JSDW_DZ", "JSDW_DZ");
		pfBgKeyMap.put("JSDW_LXR", "JSDW_LXR");
		pfBgKeyMap.put("JSDW_LXR_LXDH", "JSDW_LXR_LXDH");
		pfBgKeyMap.put("SGDW_GZ", "SGDW_GZ");
		pfBgKeyMap.put("SGDW_XMJL", "SGDW_XMJL");
		pfBgKeyMap.put("SGDW_XMJL_LXDH", "SGDW_XMJL_LXDH");
		pfBgKeyMap.put("SGDW_LXR", "SGDW_LXR");
		pfBgKeyMap.put("SGDW_LXR_LXDH", "SGDW_LXR_LXDH");
		pfBgKeyMap.put("YSGS", "YSGS");
		pfBgKeyMap.put("YSGS_LXR", "YSGS_LXR");
		pfBgKeyMap.put("YSGS_LXR_LXDH", "YSGS_LXR_LXDH");
		pfBgKeyMap.put("YSGS_KGRQ", "YSGS_KGRQ");
		pfBgKeyMap.put("YSGS_WGRQ", "YSGS_WGRQ");
		pfBgKeyMap.put("SBJZFQWCZL", "SBJZFQWCZL");
		pfBgKeyMap.put("CWZTFL", "CWZTFL");
		pfBgKeyMap.put("JCZTFL", "JCZTFL");
		pfBgKeyMap.put("ZHTFL", "ZHTFL");
		pfBgKeyMap.put("DXSTFL", "DXSTFL");
		pfBgKeyMap.put("QTTFL", "QTTFL");
		

		// 初始化头部对应的map
		pfBgKeyMapHead = new HashMap<String, String>();
		pfBgKeyMapHead.put("sn", "sn");
		pfBgKeyMapHead.put("sender", "sender");
		// pfBgKeyMapHead.put("receiver", "receiver");
		pfBgKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		pfBgfujianMap = new HashMap<String, Long>();
		pfBgfujianMap.put("FJ_01", 800l);
		pfBgfujianMap.put("FJ_02", 801l);
		pfBgfujianMap.put("FJ_03", 802l);
		pfBgfujianMap.put("FJ_04", 803l);
		pfBgfujianMap.put("FJ_05", 804l);
		pfBgfujianMap.put("FJ_06", 805l);
		pfBgfujianMap.put("FJ_07", 806l);
		pfBgfujianMap.put("FJ_08", 807l);
		pfBgfujianMap.put("FJ_09", 808l);
		pfBgfujianMap.put("FJ_10", 809l);
		pfBgfujianMap.put("FJ_11", 810l);
		pfBgfujianMap.put("FJ_12", 811l);
		pfBgfujianMap.put("FJ_13", 812l);
		pfBgfujianMap.put("FJ_14", 813l);
		pfBgfujianMap.put("FJ_15", 814l);
		pfBgfujianMap.put("FJ_16", 815l);
		pfBgfujianMap.put("FJ_17", 816l);
		pfBgfujianMap.put("FJ_18", 817l);
		pfBgfujianMap.put("FJ_19", 818l);
		pfBgfujianMap.put("FJ_20", 819l);
		pfBgfujianMap.put("FJ_21", 820l);
		pfBgfujianMap.put("FJ_22", 821l);
		pfBgfujianMap.put("FJ_23", 822l);
		pfBgfujianMap.put("FJ_24", 823l);
		pfBgfujianMap.put("FJ_25", 824l);
		pfBgfujianMap.put("FJ_26", 825l);
		pfBgfujianMap.put("FJ_27", 826l);
		pfBgfujianMap.put("FJ_28", 827l);
		pfBgfujianMap.put("FJ_29", 828l);
		pfBgfujianMap.put("FJ_30", 829l);
		pfBgfujianMap.put("FJ_31", 830l);
		pfBgfujianMap.put("FJ_32", 831l);
		pfBgfujianMap.put("FJ_33", 832l);
		pfBgfujianMap.put("FJ_34", 833l);
		pfBgfujianMap.put("FJ_35", 834l);
		pfBgfujianMap.put("FJ_36", 835l);
		pfBgfujianMap.put("FJ_37", 836l);
		pfBgfujianMap.put("FJ_38", 837l);
		pfBgfujianMap.put("FJ_39", 838l);
		pfBgfujianMap.put("FJ_40", 839l);
		pfBgfujianMap.put("FJ_41", 840l);
		pfBgfujianMap.put("FJ_42", 841l);
		pfBgfujianMap.put("FJ_43", 842l);
		pfBgfujianMap.put("FJ_44", 843l);
		pfBgfujianMap.put("FJ_45", 844l);
		pfBgfujianMap.put("FJ_46", 845l);
		pfBgfujianMap.put("FJ_47", 846l);
		pfBgfujianMap.put("FJ_48", 847l);
		pfBgfujianMap.put("FJ_49", 848l);
		pfBgfujianMap.put("FJ_50", 850l);
		pfBgfujianMap.put("FJ_51", 851l);
		pfBgfujianMap.put("FJ_52", 852l);
		pfBgfujianMap.put("FJ_53", 853l);
		pfBgfujianMap.put("FJ_54", 854l);
		pfBgfujianMap.put("FJ_55", 855l);
		pfBgfujianMap.put("FJ_56", 856l);
		pfBgfujianMap.put("FJ_57", 857l);
		pfBgfujianMap.put("FJ_58", 859l);
		pfBgfujianMap.put("FJ_59", 860l);
		pfBgfujianMap.put("FJ_60", 861l);
		pfBgfujianMap.put("FJ_61", 862l);
		pfBgfujianMap.put("FJ_62", 863l);
		pfBgfujianMap.put("FJ_63", 864l);
		pfBgfujianMap.put("FJ_64", 865l);
		pfBgfujianMap.put("FJ_65", 866l);
		pfBgfujianMap.put("FJ_66", 867l);
		pfBgfujianMap.put("FJ_67", 868l);
		pfBgfujianMap.put("FJ_68", 869l);
		pfBgfujianMap.put("FJ_69", 870l);
		pfBgfujianMap.put("FJ_70", 871l);
		pfBgfujianMap.put("FJ_71", 872l);
		pfBgfujianMap.put("FJ_72", 873l);
	}

	public void readXml(String projectPath) {
		if (pfBgKeyMap == null)
			this.initpfBgMap();
		File file = new File(pfBgfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {

			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(pfBgKeyMap, filePath,pfBgfeedbackPath, pfBgexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(pfBgKeyMapHead,files[i], pfBgfeedbackPath, pfBgexceptionPath);

						// 把数据部分保存到数据库
						YwYnJzfqwczzPfsnForm form = new YwYnJzfqwczzPfsnForm();
						for (String mapKey : pfBgKeyMap.keySet()) {
							
							form.setYslNo(parseXMLService.getMapValue(pfBgKeyMap.get("YSL_NO")));
							form.setSqdw(parseXMLService.getMapValue(pfBgKeyMap.get("SQDW")));
							
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(pfBgKeyMap.get("SQDW")),"jzfqwczzpf_bg");
							if(ywComQyxxForm != null){
								form.setQyid(ywComQyxxForm.getId());
							}

							form.setAddress(parseXMLService.getMapValue(pfBgKeyMap.get("ADDRESS")));
							form.setGcdz(parseXMLService.getMapValue(pfBgKeyMap.get("GCDZ")));
							form.setGcmc(parseXMLService.getMapValue(pfBgKeyMap.get("GCMC")));
							form.setJsdwDz(parseXMLService.getMapValue(pfBgKeyMap.get("JSDW_DZ")));
							form.setJsdwLxr(parseXMLService.getMapValue(pfBgKeyMap.get("JSDW_LXR")));
							form.setJsdwLxrLxdh(parseXMLService.getMapValue(pfBgKeyMap.get("JSDW_LXR_LXDH")));
							form.setSgdwGz(parseXMLService.getMapValue(pfBgKeyMap.get("SGDW_GZ")));
							form.setSgdwXmjl(parseXMLService.getMapValue(pfBgKeyMap.get("SGDW_XMJL")));
							form.setSgdwXmjlLxdh(parseXMLService.getMapValue(pfBgKeyMap.get("SGDW_XMJL_LXDH")));
							form.setSgdwLxr(parseXMLService.getMapValue(pfBgKeyMap.get("SGDW_LXR")));
							form.setSgdwLxrLxdh(parseXMLService.getMapValue(pfBgKeyMap.get("SGDW_LXR_LXDH")));
							form.setYsgs(parseXMLService.getMapValue(pfBgKeyMap.get("YSGS")));
							form.setYsgsLxr(parseXMLService.getMapValue(pfBgKeyMap.get("YSGS_LXR")));
							form.setYsgsLxrLxdh(parseXMLService.getMapValue(pfBgKeyMap.get("YSGS_LXR_LXDH")));
							form.setYsgsKgrq(parseXMLService.getMapValue(pfBgKeyMap.get("YSGS_KGRQ")));
							form.setYsgsWgrq(parseXMLService.getMapValue(pfBgKeyMap.get("YSGS_WGRQ")));
							form.setSbjzfqwczl(parseXMLService.getMapValue(pfBgKeyMap.get("SBJZFQWCZL")));
							form.setCwztfl(parseXMLService.getMapValue(pfBgKeyMap.get("CWZTFL")));
							form.setJcztfl(parseXMLService.getMapValue(pfBgKeyMap.get("JCZTFL")));
							form.setZhtfl(parseXMLService.getMapValue(pfBgKeyMap.get("ZHTFL")));
							form.setDxstfl(parseXMLService.getMapValue(pfBgKeyMap.get("DXSTFL")));
							form.setQttfl(parseXMLService.getMapValue(pfBgKeyMap.get("QTTFL")));

							form.setBranch("jzfqwczzpf_bg");
							form.setApprovalNo(parseXMLService.getMapValue(pfBgKeyMap.get("approvalNo")));
							form.setBusinessCode(parseXMLService.getMapValue(pfBgKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(pfBgKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setYsl(1l);
							form.setSendNotice("Y");// 是否发送通知书到窗口？Y，发送；其他，不发送
						}

						// 调用DAO，保存到数据库
						try {
							ywYnJzfqwczzPfsnbgService.save(form);
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									pfBgKeyMapHead, pfBgfeedbackPath, pfBgexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, pfBgfujianpath, pfBgfeedbackPath, pfBgexceptionPath, pfBgKeyMapHead,
								files, pfBgfujianMap, i, form.getQyid(), form.getId(), "jzfqwczzpf_bg");
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								pfBgKeyMapHead, pfBgfeedbackPath, pfBgexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, pfBgbackPath + fileName,fileName);
				}
			}
		}
	}
}
